const errorTypes = require('../constants/error_types')
const errHandler = (err, ctx) => {
  let status, message
  switch (err.message) {
    case errorTypes.NAME_OR_PASSWORD_IS_REQUIRED:
      status = 400
      message = '用户名或者密码不能为空···'
      break
    case errorTypes.USER_ALREADY_EXISTS:
      status = 409
      message = '当前用户名已存在请重新输入'
      break
    case errorTypes.USER_NOT_EXISTS:
      status = 400
      message = '当前用户不存在'
      break
    case errorTypes.USERNAME_OR_PASSWORD__WRONG:
      status = 400
      message = '用户名或密码错误'
      break
    case errorTypes.UNAUTHORIZATION:
      status = 401
      message = '无效Token'
      break
    case errorTypes.NO_PERMISSION:
      status = 401
      message = '不具备操作权限'
      break
    default:
      status = 404
      message = 'NOT FOUND'
  }
  ctx.status = status
  ctx.body = message
}
module.exports = {
  errHandler,
}
